package com.sita.tboard.hitchhike.pending;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.malinskiy.superrecyclerview.OnMoreListener;
import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.tboard.hitchhike.HitchhikeContentActivity;
import com.sita.tboard.hitchhike.bean.TicketModel;
import com.sita.tboard.hitchhike.listeners.OnGetTicketsListener;
import com.sita.tboard.hitchhike.helper.HitchhikeNetworkHelper;
import com.sita.tboard.view.MyDividerItemDecoration;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.trinea.android.common.util.ToastUtils;

public class DriverPendingTicketsActivity extends HitchhikeContentActivity implements SwipeRefreshLayout.OnRefreshListener, OnMoreListener {

    @Bind(R.id.ticket_list)
    SuperRecyclerView mListView;

    @Bind(R.id.toolbar)
    Toolbar mToolbar;

    private DriverPendingTicketsAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private int currPage = 0;

    public DriverPendingTicketsActivity() {
    }

    @SuppressWarnings("unused")
    public static Intent newIntent() {
        Intent intent = new Intent(GlobalContext.getGlobalContext(), DriverPendingTicketsActivity.class);
        return intent;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hitchhike_driver_pending_trips);
        ButterKnife.bind(this);

        customizeToolbar(mToolbar, R.string.pending_plan);

        mLinearLayoutManager = new LinearLayoutManager(this);
        mListView.setLayoutManager(mLinearLayoutManager);
        Drawable drawable = new ColorDrawable(getResources().getColor(R.color.divider_list));
        mListView.addItemDecoration(new MyDividerItemDecoration(drawable, true, true));

        mAdapter = new DriverPendingTicketsAdapter(this);
        mListView.setAdapter(mAdapter);

        mListView.setOnMoreListener(this);
        mListView.setRefreshListener(this);
        if (currPage == 0) {
            requestData();
        }
    }

    @Override
    public void onRefresh() {
        currPage = 0;
        requestData();
    }

    @Override
    public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {
        currPage += 1;
        requestData();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void requestData() {
        HitchhikeNetworkHelper.INSTANCE.getDriverUnfinishedTickets(currPage, 10, new OnGetTicketsListener() {
            @Override
            public void onSuccess(ArrayList<TicketModel> items) {
                if (currPage == 0) {
                    mAdapter.setItems(items);
                    mListView.setRefreshing(false);
                    mLinearLayoutManager.scrollToPosition(0);
                } else {
                    mAdapter.append(items);
                }
            }

            @Override
            public void onFailure(Throwable throwable) {
                ToastUtils.show(GlobalContext.getGlobalContext(), com.sita.tboard.util.TextUtils.getThrowableMessage(throwable));
            }
        });
    }

}
